/*
 * Pratama Nur Wijaya (c) 2013 
 * 
 * Project       : Sound Of Indonesia
 * Filename      : Scene_Menu_Pilih_Alat_Musik.java
 * Creation Date : Apr 9, 2013 time : 9:34:36 PM
 *
 */

package id.pratama.soundofindonesia.scene;

import id.pratama.soundofindonesia.IManageableScene;
import id.pratama.soundofindonesia.SceneManager;
import id.pratama.soundofindonesia.Utils;
import id.pratama.soundofindonesia.config.IApps_Data;
import id.pratama.soundofindonesia.config.IApps_Define;
import id.pratama.soundofindonesia.data.IGraphic_Data;
import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

import android.graphics.Color;
import android.graphics.Typeface;

public class Scene_Menu_Pilih_Alat_Musik extends Scene implements IManageableScene, IApps_Data, IApps_Define,
		IGraphic_Data, IOnAreaTouchListener
{
	private BaseGameActivity			activity;
	private SceneManager				sceneManager;

	public static final int				TYPE_ANGKLUNG	= 0;
	public static final int				TYPE_GAMELAN	= 1;

	private BitmapTextureAtlas			texMenuBg;
	private ITextureRegion				regMenuBg;
	private Sprite						sprMenuBg;

	private BuildableBitmapTextureAtlas	atlasMenuBtn;
	private ITextureRegion				regMenuBtnAngklung;
	private ITextureRegion				regMenuBtnGamelan;
	private Sprite						sprMenuBtnAngklung;
	private Sprite						sprMenuBtnGamelan;

	private Font						font;
	private Text						teks;

	public Scene_Menu_Pilih_Alat_Musik(BaseGameActivity activity)
	{
		this.activity = activity;
		sceneManager = SceneManager.getInstance();
		loadResources();
		applyResources();
		registerUpdateHandler();
	}

	@Override
	public void loadResources()
	{
		if (texMenuBg == null)
			texMenuBg = new BitmapTextureAtlas(activity.getTextureManager(), MENU_UTAMA_BG_TEX_WIDTH,
					MENU_UTAMA_BG_TEX_HEIGHT, Utils.getTextureOptions());

		if (atlasMenuBtn == null)
			atlasMenuBtn = new BuildableBitmapTextureAtlas(activity.getTextureManager(), MENU_BTN_BUILDABLE_TEX_WIDTH,
					MENU_BTN_BUILDABLE_TEX_HEIGHT, Utils.getTextureOptions());

		font = FontFactory.create(activity.getFontManager(), activity.getTextureManager(), 256, 256,
				Typeface.create(Typeface.DEFAULT, Typeface.BOLD), Utils.getRatio(50), Color.WHITE);

		regMenuBg = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texMenuBg, activity, MENU_UTAMA_BG, 0, 0);
		regMenuBtnAngklung = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasMenuBtn, activity.getAssets(),
				MENU_BTN_ANGKLUNG);
		regMenuBtnGamelan = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasMenuBtn, activity.getAssets(),
				MENU_BTN_GAMELAN);

		loadBlackPawn(atlasMenuBtn);

		texMenuBg.load();
		font.load();
	}

	@Override
	public void applyResources()
	{
		if (sprMenuBg == null)
			sprMenuBg = new Sprite(0, 0, RES_WIDTH, RES_HEIGHT, regMenuBg, activity.getVertexBufferObjectManager());

		if (sprMenuBtnAngklung == null)
			sprMenuBtnAngklung = new Sprite(0, 0, Utils.getRatioWidth(MENU_BTN_ANGKLUNG_WIDTH),
					Utils.getRatioHeight(MENU_BTN_ANGKLUNG_HEIGHT), regMenuBtnAngklung,
					activity.getVertexBufferObjectManager());

		if (sprMenuBtnGamelan == null)
			sprMenuBtnGamelan = new Sprite(0, 0, Utils.getRatioWidth(MENU_BTN_GAMELAN_WIDTH),
					Utils.getRatioHeight(MENU_BTN_GAMELAN_HEIGHT), regMenuBtnGamelan,
					activity.getVertexBufferObjectManager());

		if (teks == null)
			teks = new Text(0, 0, font, "Pilih Alat Musik", activity.getVertexBufferObjectManager());
		teks.setPosition(RES_WIDTH / 2 - teks.getWidth() / 2, Utils.getRatio(10));

		sprMenuBtnAngklung.setPosition(Utils.getRatio(200), RES_HEIGHT / 2 - sprMenuBtnAngklung.getHeight() / 2);
		sprMenuBtnGamelan.setPosition(Utils.getRatio(500), RES_HEIGHT / 2 - sprMenuBtnGamelan.getHeight() / 2);
		attachChild(sprMenuBg);
		sprMenuBg.attachChild(sprMenuBtnAngklung);
		sprMenuBg.attachChild(sprMenuBtnGamelan);
		sprMenuBg.attachChild(teks);

		setOnAreaTouchListener(this);
		this.registerTouchArea(sprMenuBtnAngklung);
		this.registerTouchArea(sprMenuBtnGamelan);

	}

	@Override
	public void registerUpdateHandler()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void unloadResources()
	{
		// TODO Auto-generated method stub

	}

	/*
	 * method for loadBlackPawn \m/
	 */
	private void loadBlackPawn(BuildableBitmapTextureAtlas tex)
	{
		try
		{
			tex.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
			tex.load();
		} catch (final TextureAtlasBuilderException e)
		{
			// TODO: handle exception

		}
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY)
	{
		if (SceneManager.currentScene == SceneManager.SCENE_MENU_PILIH_ALAT_MUSIK)
		{
			if (pTouchArea.equals(sprMenuBtnAngklung))
			{
				Utils.TRACE("Pencet btn angklung");
				sceneManager.setScene(SceneManager.SCENE_MENU_PLAY_ALAT_MUSIK, TYPE_ANGKLUNG);
			} else if (pTouchArea.equals(sprMenuBtnGamelan))
			{
				Utils.TRACE("Pencet btn gamelan");
				sceneManager.setScene(SceneManager.SCENE_MENU_PLAY_ALAT_MUSIK, TYPE_GAMELAN);
			}
		}
		return false;
	}

}
